﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace DDD4Fun.Common
{
    class CDispose
    {
    }

    class SampleClass : IDisposable
    {

        private IntPtr nativeResource = Marshal.AllocHGlobal(100);

        private List<string> managedResource = new List<string>() { "ss" };

        #region IDisposable Members

        private bool disposed = false;

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        public void Close()
        {
            Dispose();
        }

        ~SampleClass()
        {
            Dispose(false);
        }

        protected virtual void Dispose(bool dispossing)
        {
            if (disposed) return;

            if (dispossing)
            {
                if (managedResource != null)
                {
                    managedResource = null;
                }
            }

            if (nativeResource != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(nativeResource);
                nativeResource = IntPtr.Zero;
            }

            disposed = true;
        }

        #endregion
    }
}
